package com.plugin.sproutcore.lang;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.plugin.sproutcore.util.SystemUtil;

/**
 * 
 * Based on <a href="http://snippets.dzone.com/posts/show/6335">Java System Call platform independant</a>
 * 
 * @author Michael
 *
 */
public class ProcessFactory 
{
	private static IExecutionEnvironment environmentFactory;
	
	public static ProcessBuilder generate(
			List<String> command, 
			File dir)
	{
		List<String> shellCommand = new ArrayList<String>();
		
		shellCommand.addAll(getExecEnvFactory().generateShell().getShellCommand());
		shellCommand.addAll(getExecEnvFactory().generateCommandLine(command));
		
		ProcessBuilder builder = new ProcessBuilder(shellCommand);
		builder.directory(dir);
		return builder;
	}
	
	private static IExecutionEnvironment getExecEnvFactory()
	{
		if (environmentFactory == null) 
		{
			int os = SystemUtil.os();
			if(os == SystemUtil.WINDOWS)
			{
				environmentFactory = new WindowsExecutionEnvironment();
			}
			else
			{
				environmentFactory = new UnixExecutionEnvironment();
			}
		}
		return environmentFactory;
	}
}
